package com.jmb.api.web.controller.permission;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmb.api.common.result.Result;
import com.jmb.api.entity.Permission;
import com.jmb.api.vo.permission.PermissionQueryVO;
import com.jmb.api.vo.permission.PermissionVO;
import com.jmb.api.web.service.PermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jdk.jfr.Unsigned;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限管理")
@RestController()
@RequestMapping("/web/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @Operation(summary = "新增更新权限")
    @PostMapping("/saveOrUpdatePermission")
    public Result saveOrUpdatePermission(@RequestBody Permission permission){
        permissionService.saveOrUpdatePermission(permission);
        return Result.ok("新增成功");
    }

    /**
     * 分页查询
     * @param permissionQueryVO 查询参数
     * @return
     */
    @Operation(summary = "权限列表")
    @GetMapping("/listPermission")
    public Result<IPage<PermissionVO>> listPermission(PermissionQueryVO permissionQueryVO){
        IPage<PermissionVO> page = new Page<>(permissionQueryVO.getPageNum(),permissionQueryVO.getPageSize());
        IPage<PermissionVO> permissionVOList = permissionService.listPermission(page, permissionQueryVO);
        return Result.ok(permissionVOList);
    }

    @Operation(summary = "删除权限")
    @DeleteMapping("/deletePermission")
    public Result deletePermission(@RequestParam Long id){
        permissionService.deletePermission(id);
        return Result.ok();
    }

    @Operation(summary = "权限树")
    @GetMapping("/listPermissionTree")
    public Result<List<PermissionVO>> listPermissionTree(){
        return Result.ok(permissionService.listPermissionTree());
    }
}
